package com.my.service.imp;

import com.my.dao.GoodsDao;
import com.my.pojo.Goods;
import com.my.sender.ProductSender;
import com.my.service.IGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


@Service
public class GoodsServiceImpl implements IGoodsService {
    private GoodsDao goodsDao;

    @Autowired
    public void setGoodsDao(GoodsDao goodsDao) {
        this.goodsDao = goodsDao;
    }

    private ProductSender productSender;

    @Autowired
    public void setProductSender(ProductSender productSender) {
        this.productSender = productSender;
    }

    @Override
    public Integer addGoods(Goods goods) {
        if (goodsDao.findOne(
                (root, cq, cb) -> cb.and(
                        cb.equal(root.get("business"), goods.getBusiness().getBusinessId()),
                        cb.equal(root.get("name"), goods.getName())
                )) != null) {
            return -1;
        } else {
            Goods res = goodsDao.save(goods);
            if (res != null) {
                productSender.stockAdd(res.getGoodsId(), res.getStock(), "商家入库");
            }
            return res == null ? -2 : res.getGoodsId();
        }
    }

    @Override
    public Integer deleteGoods(Integer goodsId) {
        Goods goods = goodsDao.getOne(goodsId);
        if (goods == null) {
            return -1;
        }
        goods.setStatus(-1);

        return goodsDao.save(goods) == null ? -1 : 1;
    }

    @Override
    public Integer reverseGoods(Integer goodsId) {
        Goods goods = goodsDao.getOne(goodsId);
        if (goods == null) {
            return -1;
        }
        goods.setStatus(0);

        return goodsDao.save(goods) == null ? -1 : 0;
    }

    @Override
    public Integer updateGoods(Goods goods,Integer oriStock) {
        int stock = goods.getStock();
        Goods res = goodsDao.save(goods);
        if (res != null) {
            productSender.stockAdd(goods.getGoodsId(), res.getStock() - oriStock, "商家更新物品数量");
        }
        return res == null ? -1 : 0;
    }

    @Override
    public Page<Goods> showGoods(Pageable pageable, String context, Integer businessId) {
        return goodsDao.findAll(
                (root, cq, cb) -> cb.and(cb.equal(root.get("business"), businessId), cb.like(root.get("name"), "%" + context + "%")), pageable);
    }

    @Override
    public Page<Goods> showGoods(Pageable pageable, String context) {
        return goodsDao.findAll(
                (root, cq, cb) -> cb.like(root.get("name"), "%" + context + "%"), pageable);
    }

    @Override
    public Goods selById(Integer id) {
        return this.goodsDao.findOne(id);
    }

    @Override
    public Integer buyGoods(Integer num, Integer goodsId, Integer userId) {
        Goods goods = goodsDao.findOne(goodsId);
        if(goods == null || goods.getStatus() != 0) {
            return -1;
        }

        if(goods.getStock() < num) {
            return -2;
        }

        goods.setStock(goods.getStock() - num);
        if(goodsDao.save(goods) == null) {
            return -3;
        }


        String msg = "用户" + userId + "下单了" + num + "件商品";
        productSender.stockAdd(goodsId, -num, msg);
        return 1;
    }
}
